<template>
    <div class="default-main">
        <el-row v-loading="state.loading" :gutter="20">
            <el-col class="xs-mb-20" :xs="24" :sm="16">
                <el-form
                    v-if="!state.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="onSubmit()"
                    :model="state.form"
                    :rules="state.rules"
                    :label-position="'top'"
                    :key="state.formKey"
                >
                    <div class="config-tab-pane" v-for="(item, key) in state.config" :key="key" :name="key" :label="item.title">
                        <div class="config-form-item">

                            <FormItem
                                v-if="item.type == 'number'"
                                :label="item.title"
                                :type="item.type"
                                v-model="state.form[item.name]"
                                :attr="{ prop: item.name, ...item.extend }"
                                :input-attr="{ ...item.input_extend }"
                                :tip="item.tip"
                                :key="'number-' + item.id"
                            />

                            <!-- 富文本在dialog内全屏编辑器时必须拥有很高的z-index，此处选择单独为editor设定较小的z-index -->
                            <FormItem
                                v-else-if="item.type == 'editor'"
                                :label="item.title"
                                :type="item.type"
                                @keyup.enter.stop=""
                                @keyup.ctrl.enter="onSubmit()"
                                v-model="state.form[item.name]"
                                :attr="{ prop: item.name, ...item.extend }"
                                :input-attr="{
                                    style: {
                                        zIndex: 99,
                                    },
                                    ...item.input_extend,
                                }"
                                :tip="item.tip"
                                :key="'editor-' + item.id"
                            />

                            <FormItem
                                v-else-if="item.type == 'textarea'"
                                :label="item.title"
                                :type="item.type"
                                @keyup.enter.stop=""
                                @keyup.ctrl.enter="onSubmit()"
                                v-model="state.form[item.name]"
                                :attr="{ prop: item.name, ...item.extend }"
                                :input-attr="{ rows: 3, ...item.input_extend }"
                                :tip="item.tip"
                                :key="'textarea-' + item.id"
                            />


                            <FormItem
                                v-else
                                :label="item.title"
                                :type="item.type"
                                v-model="state.form[item.name]"
                                :attr="{ prop: item.name, ...item.extend }"
                                :input-attr="!isEmpty(item.content) ? { content: item.content, ...item.input_extend } : item.input_extend"
                                :tip="item.tip"
                                :key="'other-' + item.id"
                            />


                        </div>

                    </div>
                    <el-button type="primary" @click="onSubmit()">{{ t('Save') }}</el-button>
                </el-form>
            </el-col>

        </el-row>

        <!-- <AddFrom v-if="!state.loading" v-model="state.showAddForm" :config-group="state.configGroup" /> -->
    </div>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { ElMessageBox, ElNotification } from 'element-plus'
import { isEmpty } from 'lodash-es'
import { onActivated, onDeactivated, onMounted, onUnmounted, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import AddFrom from './add.vue'
import { del, index, postData, postSendTestMail } from '/@/api/backend/routine/config'
import FormItem from '/@/components/formItem/index.vue'
import { adminBaseRoutePath } from '/@/router/static/adminBase'
import type { SiteConfig } from '/@/stores/interface'
import { useSiteConfig } from '/@/stores/siteConfig'
import { uuid } from '/@/utils/random'
import { routePush } from '/@/utils/router'
import { buildValidatorData, type buildValidatorParams } from '/@/utils/validate'
import { closeHotUpdate, openHotUpdate } from '/@/utils/vite'
import createAxios from '/@/utils/axios'


defineOptions({
    name: 'routine/config',
})

const { t } = useI18n()
const siteConfig = useSiteConfig()

const formRef = ref<FormInstance>()

const state: {
    loading: boolean
    config: anyObj
    showAddForm: boolean
    rules: Partial<Record<string, FormItemRule[]>>
    form: anyObj
    formKey: string
} = reactive({
    loading: true,
    config: [],
    showAddForm: false,
    rules: {},
    form: {},
    formKey: uuid(),
})

const getIndex = () => {
    createAxios({
        url: '/backend/site-config/lists',
        method: 'get',
    })
        .then((res) => {
            state.config = res.data
            state.loading = false
            let formNames: anyObj = {}
            let rules: Partial<Record<string, FormItemRule[]>> = {}

            console.log('请求接口获取到的数据：')
            console.log(state.config)

            for (const key in state.config) {
                console.log('循环', key)
                console.log('循环', state.config[key]?.rule)
                console.log('循环', state.config[key].extend)
                if (state.config[key]?.rule) {
                    let ruleStr = state.config[key].rule.split(',')
                    let ruleArr: anyObj = []
                    ruleStr.forEach((item: string) => {
                        ruleArr.push(
                            buildValidatorData({ name: item as buildValidatorParams['name'], title: state.config[key].title })
                        )
                    })

                    rules = Object.assign(rules, { [state.config[key].name]: ruleArr, })
                }
                formNames[state.config[key].name] =  state.config[key].type == 'number' ? parseFloat(state.config[key].value)  : state.config[key].value
            }

            state.form = formNames
            state.rules = rules
            state.formKey = uuid()

            console.log('请求接口处理以后：')
            console.log(state.form)
            console.log(state.rules)
            console.log(siteConfig.$state)
            console.log(adminBaseRoutePath)
        })
        .catch(() => {
            state.loading = false
        })
}

const onBeforeLeave = (newTabName: string | number) => {
    if (newTabName == 'add_config') {
        state.showAddForm = true
        return false
    }
}

const onSubmit = () => {
    if (!formRef.value) return
    formRef.value.validate((valid) => {
        if (valid) {
            // 只提交当前tab的表单数据
            const formData: anyObj = {}
            for (const key in state.config) {
                formData[state.config[key].name] = state.form[state.config[key].name] ?? ''
            }

            createAxios(
                {
                    url: '/backend/site-config/save',
                    method: 'post',
                    data: formData,
                },
                {
                    showSuccessMessage: true,
                }
            ).then(() => {
                for (const key in siteConfig.$state) {
                    if (formData[key] && siteConfig.$state[key as keyof SiteConfig] != formData[key]) {
                        ;(siteConfig.$state[key as keyof SiteConfig] as any) = formData[key]
                    }
                }
            })
        }
    })
}

const onDelConfig = (config: anyObj) => {
    del([config.id]).then(() => {
        getIndex()
    })
}

const onTestSendMail = () => {
    if (!state.form.smtp_server || !state.form.smtp_port || !state.form.smtp_user || !state.form.smtp_pass || !state.form.smtp_sender_mail) {
        ElNotification({
            type: 'error',
            message: t('routine.config.Please enter the correct mail configuration'),
        })
        return false
    }

    ElMessageBox.prompt(t('routine.config.Please enter the recipient email address'), t('routine.config.Test mail sending'), {
        confirmButtonText: t('routine.config.send out'),
        cancelButtonText: t('Cancel'),
        inputPattern: /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/,
        inputErrorMessage: t('routine.config.Please enter the correct email address'),
        beforeClose: (action, instance, done) => {
            if (action === 'confirm') {
                instance.confirmButtonLoading = true
                instance.confirmButtonText = t('routine.config.Sending')
                postSendTestMail(state.form, instance.inputValue)
                    .then(() => {
                        done()
                    })
                    .catch(() => {
                        done()
                    })
            } else {
                done()
            }
        },
    })
}

onMounted(() => {
    getIndex()
    closeHotUpdate('config')
})
onActivated(() => {
    closeHotUpdate('config')
})
onDeactivated(() => {
    openHotUpdate('config')
})
onUnmounted(() => {
    openHotUpdate('config')
})
</script>

<style scoped lang="scss">
.send-test-mail {
    padding-bottom: 20px;
}
.el-tabs--border-card {
    border: none;
    box-shadow: var(--el-box-shadow-light);
    border-radius: var(--el-border-radius-base);
}
.el-tabs--border-card :deep(.el-tabs__header) {
    background-color: var(--ba-bg-color);
    border-bottom: none;
    border-top-left-radius: var(--el-border-radius-base);
    border-top-right-radius: var(--el-border-radius-base);
}
.el-tabs--border-card :deep(.el-tabs__item.is-active) {
    border: 1px solid transparent;
}
.el-tabs--border-card :deep(.el-tabs__nav-wrap) {
    border-top-left-radius: var(--el-border-radius-base);
    border-top-right-radius: var(--el-border-radius-base);
}
.el-card :deep(.el-card__header) {
    height: 40px;
    padding: 0;
    line-height: 40px;
    border: none;
    padding-left: 20px;
    background-color: var(--ba-bg-color);
}
.config-tab-pane {
    padding: 5px;
}
.config-tab-pane-add {
    width: 80%;
}
.config-form-item {
    display: flex;
    align-items: center;
    .el-form-item {
        flex: 13;
    }
    .config-form-item-name {
        opacity: 0;
        flex: 3;
        font-size: 13px;
        color: var(--el-text-color-disabled);
        padding-left: 20px;
    }
    .del-config-form-item {
        cursor: pointer;
        flex: 1;
        padding-left: 10px;
    }
    .close-icon {
        display: none;
    }
    &:hover {
        .config-form-item-name {
            opacity: 1;
        }
        .close-icon {
            display: block;
            color: var(--el-text-color-disabled) !important;
        }
    }
}
.config_quick_entrance {
    margin-left: 10px;
    margin-bottom: 10px;
}
@media screen and (max-width: 768px) {
    .xs-mb-20 {
        margin-bottom: 20px;
    }
}
</style>
